package leetcode100;

import java.util.Arrays;

public class lc032 {
    public static void main(String[] args) {
        int[] nums = {3,2,1};
        new lc032().nextPermutation(nums);
        System.out.println("Arrays.toString(nums) = " + Arrays.toString(nums));
    }

    public void nextPermutation(int[] nums) {

        int i = nums.length - 2;
        while (i >= 0) {
            if (nums[i] < nums[i + 1]) {
                break;
            }
            i--;
        }
        if (i>=0) {
            int j = nums.length - 1;
            while (j >= 0) {
                if (nums[j] > nums[i]) {
                    break;
                }
                j--;
            }

            int temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;
        }
        Arrays.sort(nums,i+1,nums.length);
    }
}
